package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyBaseQuery;
import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.model.TbyEducationStele;
import com.zl.tby.repository.TbyEducationSteleRepo;
import com.zl.tby.service.ITbyEducationSteleService;
import com.zl.tby.vo.TbyEducationSteleForm;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 科研宣教-解说碑
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyEducationSteleServiceImpl implements ITbyEducationSteleService {

    @Db
    private TbyEducationSteleRepo tbyEducationSteleRepo;

    @Override
    @Tran
    public R<TbyEducationStele> add(TbyEducationSteleForm form) {
        TbyEducationStele entity = form.createEntity();
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyEducationSteleRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyEducationStele> update(TbyEducationSteleForm form) {
        TbyEducationStele entity = this.get(form.getId());
        if (entity == null) {
            return R.failed("数据不存在");
        }
        form.updateEntity(entity);
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyEducationSteleRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public R<Void> delete(Long id) {
        // TODO 验证是否被引用
        int flag = tbyEducationSteleRepo.deleteById(id);
        return flag > 0 ? R.ok() : R.failed("删除失败");
    }

    @Override
    public TbyEducationStele get(Long id) {
        return tbyEducationSteleRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyEducationStele> page(TbyBaseQuery query) {
        IPage<TbyEducationStele> iPage = tbyEducationSteleRepo.selectPage(query.getOffset(), query.getSize(), mp -> {});
        return new TbyPageResult(iPage, query);
    }

    @Override
    public List<TbyEducationStele> list() {
        return tbyEducationSteleRepo.selectList(mapperWhereQ -> {});
    }
}
